import { GameData } from "./GameData";
import { SocketMag, SocketType } from "./SocketMag";
import SomeoneMenu from "./SomeoneMenu";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SettlementUI extends cc.Component {


    public Gr_show: cc.Node = null;
    public btn_ok: cc.Button = null;
    public btn_exit: cc.Button = null;
    public lab_title: cc.Label = null;
    public lab_host: cc.Label = null;

    @property(cc.Prefab) public Prefab_List: cc.Prefab = null;

    onLoad() {
        this.Gr_show = this.node.getChildByName('Gr_show');
        this.btn_ok = this.node.getChildByName('btn_ok').getComponent(cc.Button);
        this.btn_exit = this.node.getChildByName('btn_exit').getComponent(cc.Button);
        this.lab_title = this.node.getChildByName('lab_title').getComponent(cc.Label);
        this.lab_host = this.node.getChildByName('lab_host').getComponent(cc.Label);
    }

    start() {

        this.btn_ok.node.on(cc.Node.EventType.TOUCH_END, () => {
            //准备下一句
            this.node.destroy();
            SocketMag.getInstance().SendMsg({ userid: GameData.Inst().m_nUserid }, SocketType.readyGame);
        }, this);

        this.btn_exit.node.on(cc.Node.EventType.TOUCH_END, () => {
            //返回自己的房间
            this.node.destroy();
            SocketMag.getInstance().SendMsg({ userid: GameData.Inst().m_nUserid }, SocketType.returnSelfRoom);
            SomeoneMenu.Inst.ReadyGame();
        }, this);

    }

    public OpenListInfo(PlayerList: any[]) {
        let isMaster: boolean = GameData.Inst().m_nCurRoomMasterid === GameData.Inst().m_nUserid;
        if (isMaster) {
            this.btn_exit.node.active = false;
            this.btn_ok.node.x = 0;
        }
        else{
            this.btn_exit.node.active = true;
            this.btn_ok.node.x = 161;
        }

        this.lab_title.string = '结算';
        this.lab_host.string = '主持人：' + GameData.Inst().GetHostName();
        let i = 0;
        PlayerList.forEach(value => {
            let listNode = cc.instantiate(this.Prefab_List);
            listNode.x = -450;
            listNode.active = false;
            this.Gr_show.addChild(listNode);
            listNode.getChildByName('lab_sort').getComponent(cc.Label).string = (i + 1).toString();
            listNode.getChildByName('lab_name').getComponent(cc.Label).string = value.name;
            listNode.getChildByName('lab_ConTime').getComponent(cc.Label).string = value.nConTime + 's';
            listNode.getChildByName('lab_score').getComponent(cc.Label).string = value.nScore + '';

            if (GameData.Inst().m_nUserid === value.userid) {
                this.lab_title.string = value.nScore > 0 ? '胜利' : '失败';
            }

            this.scheduleOnce(() => {
                listNode.active = true;
                cc.tween(listNode).to(0.3, { x: 0 }).start();
            }, i * 0.2);
            i++;
        });
    }
}
